package com.zhkj.txg.module.order.ui

import android.app.Activity
import android.content.Intent
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.zhkj.lib.base.ViewModelActivity
import com.zhkj.lib.http.HttpResponseStatus
import com.zhkj.lib.utils.AccountManager
import com.zhkj.txg.R
import com.zhkj.txg.module.order.adapter.OrderShippingAdapter
import com.zhkj.txg.module.order.vm.OrderViewModel
import com.zhkj.txg.utils.ImageUtils
import kotlinx.android.synthetic.main.activity_order_shipping.*

class OrderShippingActivity : ViewModelActivity<OrderViewModel>() {

    companion object {
        private val ORDER_ID = "orderId"
        private val ORDER_GROUP = "ORDER_GROUP"//拼团订单
        fun start(activity: Activity, orderId: Long, isGroup:Boolean = false) {
            val intent = Intent(activity, OrderShippingActivity::class.java)
                .putExtra(ORDER_ID, orderId)
            if (isGroup) {
                intent.putExtra(ORDER_GROUP, isGroup)
            }
            activity.startActivity(intent)
        }
    }

    var isGroup: Boolean = false
    override fun setViewModel(): OrderViewModel = createViewModel()
    override fun statusTitleBar(): View? = titleOrderShipping
    override val layoutResId: Int = R.layout.activity_order_shipping

    private val shippingAdapter = OrderShippingAdapter().apply {
        headerWithEmptyEnable = true
    }
    private lateinit var ivShipping: ImageView
    private lateinit var tvShippingName: TextView
    private lateinit var tvShippingNo: TextView

    override fun initUI() {
        with(rvOrderShipping) {
            layoutManager = LinearLayoutManager(this@OrderShippingActivity)
            adapter = shippingAdapter
        }
        with(shippingAdapter) {
            setHeaderView(createHeaderView())
            setEmptyView(R.layout.layout_empty_data)
        }
    }

    override fun initData() {
        val orderId = intent.getLongExtra(ORDER_ID,0)
        viewModel.orderShippingDetail(AccountManager.getToken(), orderId, isGroup)
    }

    override fun initViewModel() {
        isGroup = intent.getBooleanExtra(ORDER_GROUP, false)
        // 快递详情观察者
        viewModel.orderShippingLiveData.observe(this, Observer { response ->
            handlerResponseStatus(response.status, response.statusTip)
            if (response.status == HttpResponseStatus.Status.SUCCESS) response.response?.data?.let { data ->
                data.image?.let { ImageUtils.loadRoundCornerImage(this, ivShipping, it) }
                tvShippingName.text = "配送快递:${data.shippingName}"
                tvShippingNo.text = "快递单号:${data.shippingNo}"
            }
        })
        // 快递数据观察者
        viewModel.queryShippingLiveData.observe(this, Observer { response ->
            handlerResponseStatus(response.status, response.statusTip)
            if (response.status == HttpResponseStatus.Status.SUCCESS) response.response?.let {
                shippingAdapter.setNewData(it.data)
            }
        })
    }

    private fun createHeaderView(): View =
        layoutInflater.inflate(R.layout.item_shipping_detail, null).apply {
            ivShipping = findViewById(R.id.ivShipping)
            tvShippingName = findViewById(R.id.tvShippingName)
            tvShippingNo = findViewById(R.id.tvShippingNo)
        }
}